package com.example.studyapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.google.gson.Gson
import org.json.JSONObject

class login1 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_login1)
        val username: EditText =this.findViewById(R.id.username1)
        val password: EditText =this.findViewById(R.id.password1)
        val login: Button =this.findViewById(R.id.login1)
        val login_reg:Button=this.findViewById(R.id.login_reg)

        //登录按钮点击
        login.setOnClickListener { v->
            //创建发送结构
            val js:Jstruct=myjson1.get(json_enum_dataclass.login)
                    //Jstruct(json_enum_dataclass.login,json_enum_cs.client)
            js.client.login.username=username.text.toString()
            js.client.login.password=password.text.toString()
            val jobj=JSONObject(Gson().toJson(js))
            object : jsonInteraction(jobj){
                override fun myResposeListener(response: JSONObject) {

                    //处理返回信息
                    val serj:Jstruct=myjson1.tojstu(response.toString())
                    if(serj.error){//登陆失败
                        //username.setText(response.toString())
                        Toast.makeText(this@login1,"昵称或者密码错误",Toast.LENGTH_LONG)
                                .show()
                    }else{
                        this@login1.finish()
                    }
                }
                override fun myResposeErrorListener(err: VolleyError) {
                    Toast.makeText(this@login1,err.toString(),Toast.LENGTH_LONG)
                            .show()
                }
            }
        }


        login_reg.setOnClickListener {
            startActivity(Intent(this,register::class.java))
        }
    }
}